package com.hengxinguotong.zhihuichengjian.ui.material;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.MaterialUseListAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Material;
import com.hengxinguotong.zhihuichengjian.bean.MaterialListRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 材料领用列表
 * Created by liux on 2017/3/15.
 */

public class MaterialUseListActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.material_use_lv)
    PullToRefreshListView materialUseLv;
    @BindView(R.id.add)
    HXTextView add;
    private int pageIndex = 1;
    private String type;
    private List<Material> materials;
    private MaterialUseListAdapter materialUseListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_use_list);
        ButterKnife.bind(this);
        if (hasAdd()) {
            add.setVisibility(View.VISIBLE);
        }
        materialUseLv.setMode(PullToRefreshBase.Mode.BOTH);
        materialUseLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                getMaterialInfo(null);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getMaterialInfo(null);
            }
        });
        materialUseLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Material material = materials.get(position - 1);
                Intent intent = new Intent(MaterialUseListActivity.this, MaterialUseActivity.class);
                intent.putExtra("material", material);
                startActivity(intent);
            }
        });
        getMaterialInfo(getResources().getString(R.string.loading));

    }


    private void getMaterialInfo(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this,"constructionid"));
            jsonObject.put("pageNum", pageIndex);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/material/queryUseMaterialList/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (materialUseLv.isRefreshing()) {
                    materialUseLv.onRefreshComplete();
                }

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    MaterialListRes materialListRes = gson.fromJson(json, MaterialListRes.class);
                    addData(materialListRes.getValue());
                }
            }

            @Override
            public void onFailure(String json) {
                if (materialUseLv.isRefreshing()) {
                    materialUseLv.onRefreshComplete();
                }
            }
        });
    }


    private void addData(List<Material> datas) {
        if (pageIndex == 1) {
            if(datas.size()==0){
                showEmptyView(materialUseLv);
            }else{
                dismissEmptyView();
            }
            materials = datas;
            materialUseListAdapter = new MaterialUseListAdapter(materials, this);
            materialUseLv.setAdapter(materialUseListAdapter);
            if (datas.size() == Utils.ONCECOUNT) {
                materialUseLv.setMode(PullToRefreshBase.Mode.BOTH);
            }
        } else {
            materials.addAll(datas);
            materialUseListAdapter.notifyDataSetChanged();
        }

        if (datas.size() < Utils.ONCECOUNT) {
            materialUseLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        }

        if (datas.size() != 0) {
            pageIndex++;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1 && materialUseLv != null) {
            materialUseLv.setRefreshing();
        }
    }

    @OnClick({R.id.iv_back, R.id.add})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.add:
                intent = new Intent(MaterialUseListActivity.this, MaterialUseActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

}
